import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import Router from '../router'
import { resetRouter } from '@/router'
import { removeToken } from '@/utils/auth'
import BaseUrlConfig from '@/api/config'


const service = axios.create({
    baseURL: '/', // url = base url + request url
    timeout: 30000 // request timeout,
})

service.interceptors.request.use(

 
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(

    response => {
        const res = response.data
        if (res.code === 10100){
            Message({
                message: 'token失效请重新登录',
                type: 'error',
                duration: 5 * 1000
            })
            if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'test'){
                top.location.href = 'http://10.28.128.65/front/login'
            }
            if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'pre'){
                top.location.href = 'http://10.28.136.202:8101/prefront/login'
            }
            if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'prod'){
                top.location.href = 'http://10.1.151.134/front/login'
            }
            console.log('失败  直接跳转人管首页')
          }
        if(res.code === 10086){
            return res
        }
 
 
        if (res.code === 1) {
            if (res.data && res.data.errorCode === 50003) {
                removeToken()  // must remove  token  first
                resetRouter()
                if (sessionStorage.getItem('AGENT-Token') != '') {
                    Message({
                        message: '登录账号不匹配',
                        type: 'error',
                        duration: 5 * 1000
                    })
                    if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'test'){
                        top.location.href = 'http://10.28.128.65/front/login'
                    }
                    if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'pre'){
                        top.location.href = 'http://10.28.136.202:8101/prefront/login'
                    }
                    if (process.env.NODE_ENV === 'production' && process.env.VUE_APP_BASE_API === 'prod'){
                        top.location.href = 'http://10.1.151.134/front/login'
                    }
                    console.log('登录TOKEN报错 50003 ')
                }else{
                    Router.push({ path: '/login' })
                }
               
            }
        }
        if (!res.code) {
            return res
        }

        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 20000 && res.code !== 0 && res.code != 10100&& res.code != 10101) {
            console.log(res)
            Message({
                message: res.msg || 'Error',
                type: 'error',
                duration: 5 * 1000
            })

            // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
            if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
                // to re-login
                MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
                    confirmButtonText: 'Re-Login',
                    cancelButtonText: 'Cancel',
                    type: 'warning'
                }).then(() => {
                    store.dispatch('user/resetToken').then(() => {
                        location.reload()
                    })
                })
            }
            return Promise.reject(new Error(res.msg || 'Error'))
        } else {
            return res
        }
    },
    error => {
        console.log('err' + error) // for debug
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service